﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Medical.Business;
using Medical.Entity;
using Medical.Data;


/// <summary>
/// 管理员列表
/// </summary>
public partial class webadmin_AdminList : AdminPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitData();
        }
    }

    protected void InitData()
    {
        string strUserName = txtUserName.Text.Trim();
        string strName = txtName.Text.Trim();
        int nRecordCount = 0;
        int nPageCount = 0;
        //信息列表绑定
        DataTable dt = new BisAdminInfo().GetAdminList(strUserName, strName, anpPage.CurrentPageIndex, anpPage.PageSize, ref nPageCount, ref nRecordCount);
        anpPage.RecordCount = nRecordCount;
        if (nRecordCount == 0)
        {
            noData.Style["display"] = "block";
        }
        else
        {
            noData.Style["display"] = "none";
        }
        rptAdminInfo.DataSource = dt;
        rptAdminInfo.DataBind();
        dt.Dispose();
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return 1;
    }


    /// <summary>
    /// 列表绑定
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptAdminInfo_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            //删除信息
            string strSql = "select count([ID]) from [Article] where [AdminID]=" + e.CommandArgument.ToString();
            Object obj=SqlHelper.ExecuteScalar(SqlHelper.database,CommandType.Text,strSql);
            if (Convert.ToInt32(obj) > 0)
            {
                Until.Alert("该管理员与其他信息有关联无法删除，请停用该帐号!");
                return;
            }
            strSql = "select count([ID]) from [Feedback] where [AdminID]=" + e.CommandArgument.ToString();
            obj = SqlHelper.ExecuteScalar(SqlHelper.database, CommandType.Text, strSql);
            if (Convert.ToInt32(obj) > 0)
            {
                Until.Alert("该管理员与其他信息关联无法删除，请停用该帐号!");
                return;
            }
            bool result = new BisAdminInfo().Delete(Convert.ToInt32(e.CommandArgument));
            Until.Alert(result ? "删除成功！" : "删除失败！");
            InitData();
        }
        else if (e.CommandName == "Lock")
        {
            //设置状态
            string[] strItem = e.CommandArgument.ToString().Split(',');
            bool result = new BisAdminInfo().SetLock(Convert.ToInt32(strItem[0]), !Convert.ToBoolean(strItem[1]));
            Until.Alert(result ? "设置成功！" : "设置失败！");
            InitData();
        }
    }

    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="src"></param>
    /// <param name="e"></param>
    protected void anpPage_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        anpPage.CurrentPageIndex = e.NewPageIndex;
        InitData();
    }

    /// <summary>
    /// 列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptAdminInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
         DataTable dt = (DataTable)((Repeater)sender).DataSource;
         Literal ltlEdite = e.Item.FindControl("ltlEdite") as Literal;
         if (dt.Rows[e.Item.ItemIndex]["ID"].ToString() == "1")
         {
             ImageButton btnDelete = e.Item.FindControl("btnDelete") as ImageButton;
             ImageButton imgLock = e.Item.FindControl("imgLock") as ImageButton;
             imgLock.Visible = false;
             ltlEdite.Visible = false;
             btnDelete.Visible = false;
         }
         else
         {
             ltlEdite.Text = "<a href=\"javascript:ShowDialog(1," + dt.Rows[e.Item.ItemIndex]["ID"].ToString() + ")\"><img alt=\"编辑\" src=\"images/ico-edit.gif\" /></a>";
         }
    }

    /// <summary>
    /// 刷新页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
        InitData();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        anpPage.CurrentPageIndex = 1;
        InitData();
    }
}